import os
import sys

# Add the scripts directory to the path so we can import the scraper
sys.path.append(os.path.join(os.path.dirname(__file__), 'scripts'))

from scripts.scrape_content import ContentScraper

def test_workspace_scrape():
    """
    Test the scrape_content.py script on the Workspace page to verify table formatting
    """
    # Create a test directory for the output
    test_output_dir = os.path.join(os.path.dirname(__file__), 'test_output')
    os.makedirs(test_output_dir, exist_ok=True)
    
    # Create a links file with just the Workspace page
    test_links_file = os.path.join(test_output_dir, 'test_links.txt')
    with open(test_links_file, 'w', encoding='utf-8') as f:
        # Note: We're using the same path structure but won't actually scrape from the web
        f.write('Workspace\thttps://example.com\tBasic Creation Guide/Getting Started/Using the Maker/Workspace\n')
    
    # Create a mock HTML content with a complex table similar to the Workspace page
    mock_html = '''
    <div class="text_content">
        <h1>Workspace</h1>
        <h3>MyDesk Context Menu</h3>
        <table>
            <thead>
                <tr>
                    <th>Directory</th>
                    <th>Sub-directory</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Create Folder</td>
                    <td>-</td>
                    <td>Creates a new folder.</td>
                </tr>
                <tr>
                    <td>Create Scripts</td>
                    <td>Create Component</td>
                    <td>Creates a new script component.</td>
                </tr>
                <tr>
                    <td>Create Scripts</td>
                    <td>Create EventType</td>
                    <td>Creates a new event type.</td>
                </tr>
                <tr>
                    <td>Import From</td>
                    <td>Import Scripts</td>
                    <td>Opens scripts from PC.</td>
                </tr>
                <tr>
                    <td>Import From</td>
                    <td>Import Image</td>
                    <td>Opens image files from PC.</td>
                </tr>
            </tbody>
        </table>
    </div>
    '''
    
    # Create the scraper instance
    scraper = ContentScraper(links_file=test_links_file, docs_root=test_output_dir)
    
    # Test the process_content method directly with our mock HTML
    try:
        # We don't need a real URL for this test
        local_path = 'Basic Creation Guide/Getting Started/Using the Maker/Workspace'
        scraper.process_content(mock_html, 'https://example.com', local_path)
        
        # Check if the markdown file was created successfully
        output_file = os.path.join(test_output_dir, local_path, 'Workspace.md')
        if os.path.exists(output_file):
            print(f"✅ Test passed! Output file created at: {output_file}")
            
            # Read and display the content to verify table formatting
            with open(output_file, 'r', encoding='utf-8') as f:
                content = f.read()
                print("\n📄 Generated markdown content (table section):")
                # Find the table section
                table_start = content.find('MyDesk Context Menu')
                if table_start != -1:
                    table_end = content.find('\n\n#', table_start)
                    if table_end == -1:
                        table_end = len(content)
                    print(content[table_start:table_end])
                else:
                    print("Table not found in the output")
        else:
            print(f"❌ Test failed! Output file not created at: {output_file}")
            
    except Exception as e:
        print(f"❌ Test failed with exception: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_workspace_scrape()
